package com.powernode.work.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.powernode.commons.utils.DateUtils;
import com.powernode.commons.utils.ResponseData;
import com.powernode.commons.utils.UUIDUtils;
import com.powernode.work.bean.ClueBean;
import com.powernode.work.bean.ContactsBean;
import com.powernode.work.bean.CustomerBean;
import com.powernode.work.bean.TranBean;
import com.powernode.work.dao.ClueMapper;
import com.powernode.work.dao.ContactsMapper;
import com.powernode.work.dao.CustomerMapper;
import com.powernode.work.dao.TranMapper;
import com.powernode.work.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper dao;

    /**
     * 分页显示所有的线索
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public ResponseData page(Integer pageNumber, Integer pageSize,ClueBean c) {
        Page<ClueBean> page = PageHelper.startPage(pageNumber, pageSize);
        dao.selectAll(c);
        return ResponseData.ok(page.getResult(),page.getTotal());
    }

    /**
     * 添加
     * @param c
     * @return
     */
    @Override
    public ResponseData add(ClueBean c) {
        //设置主键
        c.setId(UUIDUtils.getUUID());
        //设置创建时间
        c.setCreateTime(DateUtils.formatDateTime(new Date()));
        //插入数据
        int row = dao.insertSelective(c);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 修改
     * @param clueBean
     * @return
     */
    @Override
    public ResponseData modify(ClueBean clueBean) {
        //修i该时间
        clueBean.setEditTime(DateUtils.formatDateTime(new Date()));
        int row = dao.updateByPrimaryKeySelective(clueBean);
        if(row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 删除/批量删除
     * @param ids
     * @return
     */
    @Override
    public ResponseData remove(String ids) {
        int row = dao.delBatch(ids.split(","));
        if (row>0){
            return  ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @Override
    public ResponseData findById(String id) {
        return ResponseData.ok(dao.selectByPrimaryKey(id));
    }

    /**
     * 修改线索关联的市场活动
     * 1. 先删除线索关联的市场活动
     * 2. 再重新添加线索关联的市场活动
     * @param clueId
     * @param activityIds
     * @return
     */
    @Override
    @Transactional
    public ResponseData modifyActivityAndClue(String clueId, String activityIds) {
        dao.deleteActivityByClueId(clueId);
        //判断市场活动有无选中
        if (!StrUtil.isEmpty(activityIds)) {
            dao.insertActivityClue(clueId, activityIds.split(","));
        }
        return ResponseData.ok();
    }

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private TranMapper tranMapper;
    /**
     * 1. 根据线索的id查询线索的详细信息
     * 2.创建客户
     * 3. 创建联系人
     * 4. 根据createTran 判断是否创建交易
     * 5. 如果createTran=true 就创建交易
     * @param clueId
     * @param createTran
     * @param tran
     * @return
     */
    @Override
    @Transactional
    public ResponseData convert(String clueId, Boolean createTran,String createBy, TranBean tran) {
        //1. 根据线索的id查询线索的详细信息
        ClueBean clue = dao.selectByPrimaryKey(clueId);
        //2.创建客户
        CustomerBean customer=new CustomerBean(UUIDUtils.getUUID(),
                clue.getOwner(),clue.getCompany(),clue.getWebsite(),clue.getPhone(),createBy,
                DateUtils.formatDateTime(new Date()),null,null,clue.getContactSummary(),
                clue.getNextContactTime(),clue.getDescription(),clue.getAddress());
        customerMapper.insertSelective(customer);
        //3. 创建联系人
        ContactsBean contact=new ContactsBean(UUIDUtils.getUUID(),clue.getOwner(),
                clue.getSource(),customer.getId(),clue.getFullName(),
                clue.getAppellation(),clue.getEmail(),clue.getMphone(),
                clue.getJob(),null,createBy,
                DateUtils.formatDateTime(new Date()),null,null,
                clue.getDescription(),clue.getContactSummary(),
                clue.getNextContactTime(),clue.getAddress());
        contactsMapper.insertSelective(contact);

        //4. 根据createTran 判断是否创建交易
        if (createTran){//需要创建交易
            tran.setId(UUIDUtils.getUUID());
            tran.setOwner(clue.getOwner());
            tran.setCustomerId(customer.getId());
            tran.setSource(clue.getSource());
            tran.setContactsId(contact.getId());
            tran.setCreateBy(createBy);
            tran.setCreateTime(DateUtils.formatDateTime(new Date()));
            tran.setDescription(clue.getDescription());
            tran.setContactSummary(clue.getContactSummary());
            tran.setNextContactTime(clue.getNextContactTime());
            //5. 如果createTran=true 就创建交易
            tranMapper.insertSelective(tran);
        }

        return ResponseData.ok();
    }

}
